import { answerStore } from '@renderer/store/answer'
import { msgs } from '@renderer/store/chat'
import MarkdownIt from 'markdown-it'
import { full as emoji } from 'markdown-it-emoji'
import mdHighlight from 'markdown-it-highlightjs'
import katex from '@vscode/markdown-it-katex'
import moment from 'moment'

import { parseString } from '../ai/parseString'
import html2canvas from '../html2canvas'

const roleDict = {
  ai: '助手',
  human: '提问',
  system: '设定'
}

export default async function (
  type: 'chat' | 'ans',
  format: 'md' | 'png'
): Promise<{
  suc: boolean
  result: string
}> {
  let content = ''
  const title = (t: string) =>
    `<div style="padding:16px 0px 8px 0px"><span style="border:2px solid #869d9d; padding:3px 6px; border-radius:8px; font-size: 14px;">${t}</span></div>`
  if (type === 'chat') {
    if (msgs.length === 0) return { suc: false, result: '无无对话记录' }
    content = msgs.reduce((c, msg) => {
      return c + title(roleDict[msg.role]) + '\n' + msg.content + '\n\n'
    }, '')
  }
  if (type === 'ans') {
    if (answerStore.question === '' && answerStore.answer === '')
      return { suc: false, result: '无无对话记录' }
    content =
      title('问题') + '\n' + answerStore.question + `\n ${title('回答')}\n` + answerStore.answer
  }
  let data: string = ''
  data = parseString(content).reduce((c, item) => {
    c += '\n'
    if (item.type === 'text') {
      return c + item.value
    }
    if (item.type === 'regForFile') {
      return (
        c +
        item.value.split('\n').reduce((p, c) => {
          return p + '\n> ' + c
        }, '')
      )
    }
    if (item.type === 'regForSearch') {
      return c + '联网查询：' + item.display
    }
    if (item.type === 'regForMemo') {
      return c + '查询记忆胶囊：' + item.display
    }
    if (item.type === 'regForUrl') {
      return c + `[${item.src}](${item.src})`
    }
    if (item.type === 'regForImage') {
      return c + `<img alt="${item.filename}" src="${item.value}" class="max-w-[100%]"/>`
    }
    return c
  }, '')
  if (format === 'md') {
    data =
      'data:text/plain;charset=utf-8,' +
      encodeURIComponent(`> From [Gomoon](https://gomoon.top)\n${data}`)
  } else {
    const md = MarkdownIt({
      linkify: true,
      breaks: true,
      html: true
    })
      .use(mdHighlight)
      .use(katex)
      .use(emoji)
    const html = md.render(data)
    const dom = document.createElement('div')
    document.body.append(dom)
    dom.style.height = '100px'
    dom.style.overflow = 'scroll'
    dom.style.position = 'relative'
    dom.style.zIndex = '-1'

    dom.innerHTML = `<div class="markdown break-words text-sm bg-dark px-6 pt-4 py-8 overflow-x-visible">
    ${html}
    </div>`

    const canvas = await html2canvas(dom.children[0] as HTMLDivElement, {
      backgroundColor: '#00000000'
    })

    const canvasF = document.createElement('canvas')
    const ctx = canvasF.getContext('2d')!
    canvasF.width = canvas.width + 128
    canvasF.height = canvas.height + 208
    ctx.font = 'italic lighter 30px serif '
    ctx.fillStyle = '#869d9d'
    const svgBlob = new Blob(
      [
        `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="522.160400390625" height="43.06396484375" viewBox="0 0 522.160400390625 43.06396484375" fill="none">
<g >
<g filter="url(#filter_2_1)">
<path     fill="#000000"  d="M24.34 2.53995L24.412 9.84795L23.296 9.84795Q23.044 6.71595 21.586 5.25795Q20.128 3.79995 16.744 3.72795L10.588 3.65595L10.588 14.096L14.98 14.096Q16.564 14.096 17.248 13.196Q18.076 12.044 18.076 9.84795L19.228 9.84795L19.228 19.82L18.076 19.82Q18.076 17.696 17.212 16.508Q16.348 15.32 14.98 15.32L10.588 15.32L10.588 26.228L13.936 26.228L13.936 27.344L4.00002 27.344L4.00002 26.228L7.38402 26.228L7.38402 3.65595L4.00002 3.65595L4.00002 2.53995L24.34 2.53995ZM29.524 25.544L29.524 13.124Q29.488 11.9 28.3 11.9L26.644 11.9L26.644 10.892L32.548 10.892L32.548 14.204Q32.944 13.232 34.024 12.116Q35.644 10.496 37.444 10.496Q38.524 10.496 39.172 11.216Q39.82 11.936 39.82 12.944Q39.82 14.924 38.02 15.104Q37.3 13.52 35.644 13.52Q34.456 13.52 33.484 14.708Q32.512 15.896 32.512 17.408L32.512 25.544Q32.62 26.372 33.34 26.372L35.428 26.372L35.428 27.344L26.68 27.344L26.68 26.372L28.696 26.372Q29.344 26.372 29.524 25.544ZM56.236 12.872Q58.54 15.14 58.54 18.866Q58.54 22.592 55.984 25.112Q53.428 27.632 49.288 27.704Q46.012 27.74 43.744 25.436Q41.584 23.096 41.584 19.262Q41.584 15.428 43.996 12.98Q46.408 10.532 50.548 10.532Q53.968 10.532 56.236 12.872ZM50.728 26.552Q52.996 26.552 54.13 24.644Q55.264 22.736 55.264 19.694Q55.264 16.652 53.716 14.168Q52.132 11.72 49.792 11.72Q44.86 11.72 44.86 18.02Q44.86 21.368 46.084 23.672Q47.632 26.552 50.728 26.552ZM90.256 26.372L90.256 27.344L81.508 27.344L81.508 26.372L83.632 26.372Q84.316 26.372 84.532 25.544L84.532 17.048Q84.532 15.14 83.668 13.898Q82.804 12.656 81.256 12.656Q79.708 12.656 78.304 13.844Q76.9 14.96 76.9 15.932L76.864 25.544Q77.044 26.372 77.692 26.372L79.78 26.372L79.78 27.344L71.068 27.344L71.068 26.372L73.12 26.372Q73.804 26.372 73.984 25.544L73.984 17.048Q73.984 15.212 73.156 13.988Q72.328 12.656 70.726 12.656Q69.124 12.656 67.774 13.754Q66.424 14.852 66.424 15.932L66.424 25.544Q66.532 26.372 67.216 26.372L69.304 26.372L69.304 27.344L60.556 27.344L60.556 26.372L62.644 26.372Q63.328 26.372 63.436 25.544L63.436 13.124Q63.4 11.9 62.176 11.9L60.52 11.9L60.52 10.892L66.424 10.892L66.424 13.628Q67.144 12.332 68.332 11.576Q69.916 10.496 71.86 10.496Q75.28 10.496 76.684 13.772Q78.808 10.496 82.408 10.496Q84.604 10.496 86.026 12.026Q87.448 13.556 87.448 16.148L87.412 25.544Q87.484 26.372 88.168 26.372L90.256 26.372ZM123.54 17.588L123.54 18.74L122.316 18.74L122.316 26.084Q121.56 26.3 120.192 26.75Q118.824 27.2 118.572 27.272Q116.196 27.848 113.208 27.848Q108.708 27.848 105.36 24.104Q102.192 20.288 102.228 14.726Q102.264 9.16395 105.468 5.49195Q108.636 1.99995 113.208 1.99995Q116.304 1.99995 118.068 2.43195Q118.788 2.57595 120.732 3.33195L120.732 8.98395L119.652 8.98395Q119.4 5.85195 117.924 4.41195Q116.448 2.97195 113.388 3.11595Q106.368 3.54795 106.368 14.924Q106.368 20.072 108.204 23.294Q110.04 26.516 113.1 26.552Q114.9 26.588 117.024 25.76Q118.896 25.112 119.076 24.464Q119.256 24.104 119.256 23.024L119.256 18.74L115.188 18.74L115.188 17.588L123.54 17.588ZM141.18 12.872Q143.484 15.14 143.484 18.866Q143.484 22.592 140.928 25.112Q138.372 27.632 134.232 27.704Q130.956 27.74 128.688 25.436Q126.528 23.096 126.528 19.262Q126.528 15.428 128.94 12.98Q131.352 10.532 135.492 10.532Q138.912 10.532 141.18 12.872ZM135.672 26.552Q137.94 26.552 139.074 24.644Q140.208 22.736 140.208 19.694Q140.208 16.652 138.66 14.168Q137.076 11.72 134.736 11.72Q129.804 11.72 129.804 18.02Q129.804 21.368 131.028 23.672Q132.576 26.552 135.672 26.552ZM175.2 26.372L175.2 27.344L166.452 27.344L166.452 26.372L168.576 26.372Q169.26 26.372 169.476 25.544L169.476 17.048Q169.476 15.14 168.612 13.898Q167.748 12.656 166.2 12.656Q164.652 12.656 163.248 13.844Q161.844 14.96 161.844 15.932L161.808 25.544Q161.988 26.372 162.636 26.372L164.724 26.372L164.724 27.344L156.012 27.344L156.012 26.372L158.064 26.372Q158.748 26.372 158.928 25.544L158.928 17.048Q158.928 15.212 158.1 13.988Q157.272 12.656 155.67 12.656Q154.068 12.656 152.718 13.754Q151.368 14.852 151.368 15.932L151.368 25.544Q151.476 26.372 152.16 26.372L154.248 26.372L154.248 27.344L145.5 27.344L145.5 26.372L147.588 26.372Q148.272 26.372 148.38 25.544L148.38 13.124Q148.344 11.9 147.12 11.9L145.464 11.9L145.464 10.892L151.368 10.892L151.368 13.628Q152.088 12.332 153.276 11.576Q154.86 10.496 156.804 10.496Q160.224 10.496 161.628 13.772Q163.752 10.496 167.352 10.496Q169.548 10.496 170.97 12.026Q172.392 13.556 172.392 16.148L172.356 25.544Q172.428 26.372 173.112 26.372L175.2 26.372ZM192.192 12.872Q194.496 15.14 194.496 18.866Q194.496 22.592 191.94 25.112Q189.384 27.632 185.244 27.704Q181.968 27.74 179.7 25.436Q177.54 23.096 177.54 19.262Q177.54 15.428 179.952 12.98Q182.364 10.532 186.504 10.532Q189.924 10.532 192.192 12.872ZM186.684 26.552Q188.952 26.552 190.086 24.644Q191.22 22.736 191.22 19.694Q191.22 16.652 189.672 14.168Q188.088 11.72 185.748 11.72Q180.816 11.72 180.816 18.02Q180.816 21.368 182.04 23.672Q183.588 26.552 186.684 26.552ZM211.488 12.872Q213.792 15.14 213.792 18.866Q213.792 22.592 211.236 25.112Q208.68 27.632 204.54 27.704Q201.264 27.74 198.996 25.436Q196.836 23.096 196.836 19.262Q196.836 15.428 199.248 12.98Q201.66 10.532 205.8 10.532Q209.22 10.532 211.488 12.872ZM205.98 26.552Q208.248 26.552 209.382 24.644Q210.516 22.736 210.516 19.694Q210.516 16.652 208.968 14.168Q207.384 11.72 205.044 11.72Q200.112 11.72 200.112 18.02Q200.112 21.368 201.336 23.672Q202.884 26.552 205.98 26.552ZM235.464 26.372L235.464 27.344L226.68 27.344L226.68 26.372L228.804 26.372Q229.488 26.372 229.596 25.544L229.596 17.012Q229.596 14.924 228.516 13.754Q227.436 12.584 225.744 12.584Q224.052 12.584 222.612 13.7Q221.244 14.852 221.244 15.86L221.244 25.544Q221.352 26.372 222.072 26.372L224.16 26.372L224.16 27.344L215.376 27.344L215.376 26.372L217.464 26.372Q218.184 26.372 218.292 25.544L218.292 13.088Q218.256 11.864 217.068 11.864L215.376 11.864L215.376 10.892L221.28 10.892L221.28 11.864Q221.244 12.296 221.244 12.872L221.244 13.592Q221.532 12.908 222.756 11.864Q224.592 10.424 226.752 10.424Q232.548 10.424 232.548 16.58L232.548 25.544Q232.656 26.372 233.376 26.372L235.464 26.372ZM246.86 18.344L275.696 18.344L275.696 16.112L246.86 16.112L246.86 18.344ZM279.044 18.344L307.88 18.344L307.88 16.112L279.044 16.112L279.044 18.344ZM340.104 10.64L345.9 10.64L345.18 12.548L341.04 12.188Q342.732 13.772 342.768 15.824Q342.84 17.444 341.904 18.992Q340.968 20.54 339.492 21.296Q337.62 22.34 335.784 22.34Q335.208 22.34 334.776 22.196L333.768 23.06Q333.156 23.672 333.156 24.176Q333.156 25.004 334.092 25.328Q334.524 25.472 335.82 25.544L341.436 25.976Q345.576 26.264 345.576 29.972Q345.576 32.816 343.002 34.94Q340.428 37.064 336.072 37.064Q332.832 37.064 330.672 35.552Q328.512 34.112 328.512 31.97Q328.512 29.828 329.988 28.748Q330.96 27.92 332.544 27.704Q330.276 27.128 330.276 25.652Q330.276 24.536 331.572 23.348Q332.076 22.916 333.48 22.052Q332.4 22.016 330.924 20.684Q329.448 19.352 329.412 17.336Q329.304 14.204 331.536 12.422Q333.768 10.64 337.08 10.604Q338.988 10.604 340.104 10.64ZM336.126 21.08Q336.126 21.08 336.963 21.08Q337.8 21.08 338.826 19.91Q339.852 18.74 339.798 16.706Q339.744 14.672 338.88 13.484Q337.836 11.864 335.982 11.864Q334.128 11.864 333.192 13.16Q332.256 14.456 332.256 16.526Q332.256 18.596 333.354 19.838Q334.452 21.08 336.126 21.08ZM337.908 28.46L334.164 28.136Q333.192 28.172 332.292 29.036Q331.212 29.936 331.212 31.412Q331.212 33.644 332.634 34.778Q334.056 35.912 336.63 35.912Q339.204 35.912 341.004 34.616Q342.804 33.32 342.804 31.196Q342.804 29.72 341.58 29.18Q340.608 28.712 337.908 28.46ZM362.244 12.872Q364.548 15.14 364.548 18.866Q364.548 22.592 361.992 25.112Q359.436 27.632 355.296 27.704Q352.02 27.74 349.752 25.436Q347.592 23.096 347.592 19.262Q347.592 15.428 350.004 12.98Q352.416 10.532 356.556 10.532Q359.976 10.532 362.244 12.872ZM356.736 26.552Q359.004 26.552 360.138 24.644Q361.272 22.736 361.272 19.694Q361.272 16.652 359.724 14.168Q358.14 11.72 355.8 11.72Q350.868 11.72 350.868 18.02Q350.868 21.368 352.092 23.672Q353.64 26.552 356.736 26.552ZM396.264 26.372L396.264 27.344L387.516 27.344L387.516 26.372L389.64 26.372Q390.324 26.372 390.54 25.544L390.54 17.048Q390.54 15.14 389.676 13.898Q388.812 12.656 387.264 12.656Q385.716 12.656 384.312 13.844Q382.908 14.96 382.908 15.932L382.872 25.544Q383.052 26.372 383.7 26.372L385.788 26.372L385.788 27.344L377.076 27.344L377.076 26.372L379.128 26.372Q379.812 26.372 379.992 25.544L379.992 17.048Q379.992 15.212 379.164 13.988Q378.336 12.656 376.734 12.656Q375.132 12.656 373.782 13.754Q372.432 14.852 372.432 15.932L372.432 25.544Q372.54 26.372 373.224 26.372L375.312 26.372L375.312 27.344L366.564 27.344L366.564 26.372L368.652 26.372Q369.336 26.372 369.444 25.544L369.444 13.124Q369.408 11.9 368.184 11.9L366.528 11.9L366.528 10.892L372.432 10.892L372.432 13.628Q373.152 12.332 374.34 11.576Q375.924 10.496 377.868 10.496Q381.288 10.496 382.692 13.772Q384.816 10.496 388.416 10.496Q390.612 10.496 392.034 12.026Q393.456 13.556 393.456 16.148L393.42 25.544Q393.492 26.372 394.176 26.372L396.264 26.372ZM413.256 12.872Q415.56 15.14 415.56 18.866Q415.56 22.592 413.004 25.112Q410.448 27.632 406.308 27.704Q403.032 27.74 400.764 25.436Q398.604 23.096 398.604 19.262Q398.604 15.428 401.016 12.98Q403.428 10.532 407.568 10.532Q410.988 10.532 413.256 12.872ZM407.748 26.552Q410.016 26.552 411.15 24.644Q412.284 22.736 412.284 19.694Q412.284 16.652 410.736 14.168Q409.152 11.72 406.812 11.72Q401.88 11.72 401.88 18.02Q401.88 21.368 403.104 23.672Q404.652 26.552 407.748 26.552ZM432.552 12.872Q434.856 15.14 434.856 18.866Q434.856 22.592 432.3 25.112Q429.744 27.632 425.604 27.704Q422.328 27.74 420.06 25.436Q417.9 23.096 417.9 19.262Q417.9 15.428 420.312 12.98Q422.724 10.532 426.864 10.532Q430.284 10.532 432.552 12.872ZM427.044 26.552Q429.312 26.552 430.446 24.644Q431.58 22.736 431.58 19.694Q431.58 16.652 430.032 14.168Q428.448 11.72 426.108 11.72Q421.176 11.72 421.176 18.02Q421.176 21.368 422.4 23.672Q423.948 26.552 427.044 26.552ZM456.528 26.372L456.528 27.344L447.744 27.344L447.744 26.372L449.868 26.372Q450.552 26.372 450.66 25.544L450.66 17.012Q450.66 14.924 449.58 13.754Q448.5 12.584 446.808 12.584Q445.116 12.584 443.676 13.7Q442.308 14.852 442.308 15.86L442.308 25.544Q442.416 26.372 443.136 26.372L445.224 26.372L445.224 27.344L436.44 27.344L436.44 26.372L438.528 26.372Q439.248 26.372 439.356 25.544L439.356 13.088Q439.32 11.864 438.132 11.864L436.44 11.864L436.44 10.892L442.344 10.892L442.344 11.864Q442.308 12.296 442.308 12.872L442.308 13.592Q442.596 12.908 443.82 11.864Q445.656 10.424 447.816 10.424Q453.612 10.424 453.612 16.58L453.612 25.544Q453.72 26.372 454.44 26.372L456.528 26.372ZM462.558 23.24Q462.558 23.24 463.035 23.24Q463.512 23.24 464.178 23.906Q464.844 24.572 464.844 25.526Q464.844 26.48 464.196 27.146Q463.548 27.812 462.558 27.812Q461.568 27.812 460.902 27.146Q460.236 26.48 460.236 25.526Q460.236 24.572 460.92 23.906Q461.604 23.24 462.558 23.24ZM472.368 7.86795L474.204 7.86795L474.204 12.296L478.704 12.224L478.632 13.88L474.204 13.88L474.204 23.168Q474.204 25.58 476.076 25.58Q476.904 25.58 478.128 24.932L478.38 25.328Q477.156 27.812 474.636 27.812Q471.18 27.74 471.18 24.032L471.18 13.88L468.48 13.952L468.48 12.98Q468.732 12.908 469.164 12.8Q470.82 12.404 471.576 11.234Q472.332 10.064 472.368 7.86795ZM495.12 12.872Q497.424 15.14 497.424 18.866Q497.424 22.592 494.868 25.112Q492.312 27.632 488.172 27.704Q484.896 27.74 482.628 25.436Q480.468 23.096 480.468 19.262Q480.468 15.428 482.88 12.98Q485.292 10.532 489.432 10.532Q492.852 10.532 495.12 12.872ZM489.612 26.552Q491.88 26.552 493.014 24.644Q494.148 22.736 494.148 19.694Q494.148 16.652 492.6 14.168Q491.016 11.72 488.676 11.72Q483.744 11.72 483.744 18.02Q483.744 21.368 484.968 23.672Q486.516 26.552 489.612 26.552ZM518.16 17.732Q518.16 21.764 515.334 24.896Q512.508 28.028 508.656 27.848Q506.028 27.812 504.84 26.948L504.732 35.228Q504.912 36.02 505.56 36.02L507.684 36.02L507.684 37.028L498.792 37.028L498.792 36.02L500.916 36.02Q501.564 36.02 501.78 35.228L501.78 13.124Q501.672 11.9 500.52 11.9L498.828 11.9L498.828 10.892L504.768 10.892Q504.768 12.872 504.84 14.636Q505.416 13.448 506.316 12.584Q508.44 10.568 511.716 10.46Q514.704 10.46 516.432 12.53Q518.16 14.6 518.16 17.732ZM513.48 24.176Q515.064 22.268 515.064 19.298Q515.064 16.328 513.912 14.708Q512.652 12.8 510.132 12.8Q507.612 12.8 504.84 16.328L504.84 23.996Q506.712 26.156 509.808 26.048Q512.076 25.976 513.48 24.176Z">
</path>
</g>
</g>
<defs>
<filter id="filter_2_1" x="0" y="0" width="522.160400390625" height="43.06396484375" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
<feFlood flood-opacity="0" result="feFloodId_2_1"/>
<feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha_2_1"/>
<feOffset dx="0" dy="2"/>
<feGaussianBlur stdDeviation="2"/>
<feComposite in2="hardAlpha_2_1" operator="out"/>
<feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"/>
<feBlend mode="normal" in2="feFloodId_2_1" result="dropShadow_1_2_1"/>
<feBlend mode="normal" in="SourceGraphic" in2="dropShadow_1_2_1" result="shape_2_1"/>
</filter>
</defs>
</svg>
`
      ],
      { type: 'image/svg+xml;charset=utf-8' }
    )
    const url = URL.createObjectURL(svgBlob)
    const img = new Image()
    img.src = url
    await new Promise((res) => {
      img.onload = () => {
        console.log('图片加载完成')
        // 在 canvas 上绘制图像
        ctx.drawImage(img, canvas.width - 460, canvas.height + 120)
        // 释放 Blob URL
        URL.revokeObjectURL(url)
        res(null)
      }
    })
    // ctx.fillText('From Gomoon --- gomoon.top', canvas.width - 280, canvas.height + 140)
    ctx.shadowOffsetX = 10
    ctx.shadowOffsetY = 35
    ctx.shadowBlur = 50
    ctx.shadowColor = 'rgba(0, 0, 0, 0.25)'
    ctx.drawImage(canvas, 64, 64)

    dom.remove()

    await new Promise((res, reject) => {
      canvasF.toBlob((blob) => {
        try {
          if (blob) {
            const clipboard = navigator.clipboard
            if (clipboard) {
              clipboard.write([new ClipboardItem({ [blob.type]: blob })])
            }
            res(null)
          }
        } catch (e) {
          reject(`图片复制失败: ${(e as Error).message}`)
        }
      })
    })
    return { suc: true, result: '已复制到剪贴板' }
  }
  const a = document.createElement('a')
  a.href = data
  a.download = `对话记录${moment().format('D-H-m')}.${format}` // 设置下载的文件名
  a.click() // 触发点击事件下载图片
  a.remove()
  return { suc: true, result: '已保存至本地' }
}
